package org.snowin.utils;


import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.net.URI;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.snowin.entities.InterfaceParam;
import org.snowin.entities.InterfaceTestEntity;

public class UrlGenerator {
    String testApi = "";
    InterfaceTestEntity interfaceTest = null;
    String[] typeArray = {"int", "string"};
    boolean isRightUrl = true;
    String url = "";
    String currentCookieString = "";

    public UrlGenerator(String testApi, InterfaceTestEntity interfaceTest) {
        this.testApi = testApi;
        this.interfaceTest = interfaceTest;
    }

    public String doGet(boolean isRandom, String testApi, InterfaceTestEntity interfaceTest)
            throws Exception {
        String requestUrl = testApi;
        BufferedReader in = null;
        String result = "";
        if (!isRandom) {
            requestUrl = generateRightUrl4Get(testApi, interfaceTest
                    .getInterfaceParams());
        } else {
            requestUrl = generateRandomUrl4Get(testApi, interfaceTest
                    .getInterfaceParams());
        }
        String cookieStrings = this.currentCookieString;
        HttpResponse response = doGet(requestUrl, cookieStrings);
        String cookies = getCookie(response);
        if (!GlobalInfo.varibles.containsKey("Cookie")) {
            GlobalInfo.varibles.put("Cookie", cookies);
        } else if (cookies.length() > 1) {
            GlobalInfo.varibles.replace("Cookie", cookies);
        }
        String referUrl = "";
        List<String> cookieList = new ArrayList();
        boolean requestOk = true;
        if (response.getStatusLine().toString().contains("Moved Temporarily")) {
            requestOk = false;
        }
        while (!requestOk) {
            if (response.getStatusLine().toString().contains("Moved Temporarily")) {
                Header[] headers = response.getAllHeaders();
                for (Header location : headers) {
                    if (location.getName().equalsIgnoreCase("Set-Cookie")) {
                        cookieList.add(location.getValue());
                    }
                }
                for (Header location : headers) {
                    if (location.getName().equalsIgnoreCase("location")) {
                        referUrl = location.getValue();
                        for (String cookie : cookieList) {
                            this.currentCookieString = (this.currentCookieString + cookie.split(";")[0] + ";");
                        }
                        if (this.currentCookieString.endsWith(";")) {
                            this.currentCookieString = this.currentCookieString.substring(0, this.currentCookieString
                                    .length() - 1);
                        }
                        response = doGet(referUrl, this.currentCookieString);
                    }
                }
            } else {
                requestOk = true;
            }
        }
        in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        StringBuffer sb = new StringBuffer("");
        String line = "";
        String NL = System.getProperty("line.separator");
        while ((line = in.readLine()) != null) {
            sb.append(line + NL);
        }
        in.close();
        result = sb.toString();
        return result;
    }

    private HttpResponse doGet(String urlString, String cookieStrings) {
        HttpResponse response = null;

        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet();
        String cookieString = "";
        if (this.currentCookieString.length() > 2) {
            request.addHeader("Cookie", this.currentCookieString);
        } else if ((null != cookieString) && (cookieString.length() > 3)) {
            request.addHeader("Cookie", cookieString);
        }
        if (!GlobalInfo.varibles.containsKey("Cookie")) {
            GlobalInfo.varibles.put("Cookie", cookieStrings);
        } else if (cookieStrings.length() > 1) {
            GlobalInfo.varibles.replace("Cookie", cookieStrings);
        }
        try {
            request.setURI(new URI(urlString));

            response = client.execute(request);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return response;
    }

    public String doTest(boolean isRandom, String type) {
        String result = doTest(isRandom, type, this.testApi, this.interfaceTest);
        if (!GlobalInfo.varibles.containsKey("Response")) {
            GlobalInfo.varibles.put("Response", result);
        } else {
            GlobalInfo.varibles.replace("Response", result);
        }
        return result;
    }

    private String doTest(String type, String testApi, InterfaceTestEntity interfaceTest) {
        String result = "";
        if ((null != interfaceTest.getPreExcution()) && (!interfaceTest.getPreExcution().equals(""))) {
            result = doTest(false, type, testApi, interfaceTest);
        }
        if (type.equalsIgnoreCase("post")) {
            try {
                result = doPost(false, testApi, interfaceTest);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (type.equalsIgnoreCase("get")) {
            try {
                result = doGet(false, testApi, interfaceTest);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (type.equalsIgnoreCase("put")) {
            try {
                result = doPut(false, testApi, interfaceTest);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (type.equalsIgnoreCase("delete")) {
            try {
                result = doDelete(false, testApi, interfaceTest);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    public String doTest(boolean isRandom, String type, String testApi, InterfaceTestEntity interfaceTest) {
        String sp = System.getProperty("file.separator");
        String result = "";
        if ((null != interfaceTest.getPreExcution()) && (interfaceTest.getPreExcution().length() > 1)) {
            String preExcution = interfaceTest.getPreExcution();
            if (!preExcution.contains(".")) {
                preExcution = preExcution + ".xml";
            }
            SAXReader saxReader = new SAXReader();
            Document doc = null;
            try {
                doc = saxReader.read(new File(GlobalInfo.rootPath + sp + "wtdapicases" + sp + "interfaces" + sp + "public" + sp + preExcution));
            } catch (DocumentException e) {
                e.printStackTrace();
            }
            Element caseElem = (Element) doc.selectSingleNode("//case");
            Element e = (Element) caseElem.selectSingleNode("test");
            Element caseApiElement = (Element) caseElem.selectSingleNode("testApi");
            String sendType = caseElem.attributeValue("type");
            String testApiNew = caseApiElement.attributeValue("api");
            String realApiUrl = "";


            List<Element> paramElems = e.selectNodes("parameter");
            InterfaceTestEntity interfaceTestEntity = new InterfaceTestEntity();
            List<InterfaceParam> parrams = new ArrayList();
            for (Element el : paramElems) {
                InterfaceParam paramEntity = new InterfaceParam();
                paramEntity.setName(el.attributeValue("name"));
                paramEntity.setRandom(false);
                paramEntity.setValue(el.attributeValue("value"));
                parrams.add(paramEntity);
            }
            interfaceTestEntity.setExpectContansString(e.attributeValue("expectContansString"));
            interfaceTestEntity.setInterfaceParams(parrams);
            interfaceTestEntity.setName(e.attributeValue("name"));
            interfaceTestEntity.setPreExcution(e.attributeValue("preExcution"));
            if (GlobalInfo.varibles.containsKey("PreCookie")) {
                this.currentCookieString = ((String) GlobalInfo.varibles.get("PreCookie"));
            }
            result = doTest(sendType, testApiNew, interfaceTestEntity);
            if (!GlobalInfo.varibles.containsKey("PreResponse")) {
                GlobalInfo.varibles.put("PreResponse", result);
            } else {
                GlobalInfo.varibles.replace("PreResponse", result);
            }
            if (!GlobalInfo.varibles.containsKey("Cookie")) {
                GlobalInfo.varibles.put("Cookie", this.currentCookieString);
            }
            if (!GlobalInfo.varibles.containsKey("PreCookie")) {
                GlobalInfo.varibles.put("PreCookie", GlobalInfo.varibles.get("Cookie"));
            } else {
                GlobalInfo.varibles.replace("PreCookie", GlobalInfo.varibles.get("Cookie"));
            }
            if (GlobalInfo.varibles.containsKey("PreCookie")) {
                this.currentCookieString = ((String) GlobalInfo.varibles.get("PreCookie"));
            }
        }
        interfaceTest.setPreExcution("");
        result = doTest(type, testApi, interfaceTest);
        this.currentCookieString = "";
        if (GlobalInfo.varibles.containsKey("PreCookie")) {
            GlobalInfo.varibles.replace("PreCookie", "");
        }
        if (GlobalInfo.varibles.containsKey("Cookie")) {
            GlobalInfo.varibles.put("Cookie", "");
        }
        return result;
    }

    public String doPost(boolean isRandom, String testApi, InterfaceTestEntity interfaceTest)
            throws Exception {
        String requestUrl = testApi;
        if (!isRandom) {
            requestUrl = generateRightUrl4Get(testApi, interfaceTest.getInterfaceParams());
        } else {
            requestUrl = generateRandomUrl4Get(testApi, interfaceTest.getInterfaceParams());
        }
        String cookieStrings = "";
        cookieStrings = this.currentCookieString;
        List<String> cookieList = new ArrayList();
        HttpResponse response = doPost(requestUrl, cookieStrings);
        String cookies = getCookie(response);
        if (!GlobalInfo.varibles.containsKey("Cookie")) {
            GlobalInfo.varibles.put("Cookie", cookies);
        } else if (cookies.length() > 1) {
            GlobalInfo.varibles.replace("Cookie", cookies);
        }
        String strResult = "";
        String referUrl = "";

        boolean requestOk = true;
        if (response.getStatusLine().toString().contains("Moved Temporarily")) {
            requestOk = false;
        }
        while (!requestOk) {
            if (response.getStatusLine().toString().contains("Moved Temporarily")) {
                Header[] headers = response.getAllHeaders();
                for (Header location : headers) {
                    if (location.getName().equalsIgnoreCase("Set-Cookie")) {
                        cookieList.add(location.getValue());
                    }
                }
                for (Header location : headers) {
                    if (location.getName().equalsIgnoreCase("location")) {
                        referUrl = location.getValue();
                        for (String cookie : cookieList) {
                            this.currentCookieString = (this.currentCookieString + cookie.split(";")[0] + ";");
                        }
                        if (this.currentCookieString.endsWith(";")) {
                            this.currentCookieString = this.currentCookieString.substring(0, this.currentCookieString.length() - 1);
                        }
                        response = doPost(referUrl, this.currentCookieString);
                    }
                }
            } else {
                requestOk = true;
            }
        }
        strResult = EntityUtils.toString(response.getEntity());
        return strResult;
    }

    public HttpResponse doPost(String url, String cookieStrings)
            throws Exception {
        String requestUrl = url;
        String[] urlArrayStrings = requestUrl.split("\\?");
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(urlArrayStrings[0]);
        String cookieString = "";
        httpPost.addHeader("charset", "UTF-8");
        if (this.currentCookieString.length() > 2) {
            httpPost.addHeader("Cookie", this.currentCookieString);
        } else if ((null != cookieString) && (cookieString.length() > 3)) {
            httpPost.addHeader("Cookie", cookieString);
        }
        if ((!GlobalInfo.varibles.containsKey("Cookie")) && (!cookieStrings.equalsIgnoreCase(""))) {
            GlobalInfo.varibles.put("Cookie", cookieStrings);
        } else if (cookieStrings.length() > 1) {
            GlobalInfo.varibles.replace("Cookie", cookieStrings);
        }
        String contentType = this.interfaceTest.getContentType();
        if (contentType.contains("application/json")) {
            httpPost.addHeader("Content-type", "application/json; charset=utf-8");
            httpPost.setHeader("Accept", "application/json");
            httpPost.setEntity(new StringEntity(this.interfaceTest.getBody(), Charset.forName("UTF-8")));
        } else if (urlArrayStrings.length > 1) {
            String[] parmsArry = urlArrayStrings[1].split("&");
            List<NameValuePair> nameValuePairs = new ArrayList(parmsArry.length);
            for (int i = 0; i < parmsArry.length; i++) {
                String valueString = "";
                if (parmsArry[i].split("=").length > 1) {
                    valueString = parmsArry[i].split("=")[1];
                }
                nameValuePairs.add(new BasicNameValuePair(parmsArry[i]
                        .split("=")[0], valueString.replace("\"", "")));
            }
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
        }
        HttpResponse response = httpclient.execute(httpPost);
        return response;
    }

    public String doPut(boolean isRandom, String testApi, InterfaceTestEntity interfaceTest)
            throws Exception {
        String requestUrl = testApi;
        if (!isRandom) {
            requestUrl = generateRightUrl4Get(testApi, interfaceTest.getInterfaceParams());
        } else {
            requestUrl = generateRandomUrl4Get(testApi, interfaceTest.getInterfaceParams());
        }
        String cookieStrings = "";
        cookieStrings = this.currentCookieString;
        List<String> cookieList = new ArrayList();
        HttpResponse response = doPut(requestUrl, cookieStrings);
        String cookies = getCookie(response);
        if (!GlobalInfo.varibles.containsKey("Cookie")) {
            GlobalInfo.varibles.put("Cookie", cookies);
        } else if (cookies.length() > 1) {
            GlobalInfo.varibles.replace("Cookie", cookies);
        }
        String strResult = "";
        String referUrl = "";

        boolean requestOk = true;
        if (response.getStatusLine().toString().contains("Moved Temporarily")) {
            requestOk = false;
        }
        while (!requestOk) {
            if (response.getStatusLine().toString().contains("Moved Temporarily")) {
                Header[] headers = response.getAllHeaders();
                for (Header location : headers) {
                    if (location.getName().equalsIgnoreCase("Set-Cookie")) {
                        cookieList.add(location.getValue());
                    }
                }
                for (Header location : headers) {
                    if (location.getName().equalsIgnoreCase("location")) {
                        referUrl = location.getValue();
                        for (String cookie : cookieList) {
                            this.currentCookieString = (this.currentCookieString + cookie.split(";")[0] + ";");
                        }
                        if (this.currentCookieString.endsWith(";")) {
                            this.currentCookieString = this.currentCookieString.substring(0, this.currentCookieString.length() - 1);
                        }
                        response = doPut(referUrl, this.currentCookieString);
                    }
                }
            } else {
                requestOk = true;
            }
        }
        strResult = EntityUtils.toString(response.getEntity());
        return strResult;
    }

    public HttpResponse doPut(String url, String cookieStrings)
            throws Exception {
        String requestUrl = url;
        String[] urlArrayStrings = requestUrl.split("\\?");
        HttpClient httpclient = new DefaultHttpClient();
        HttpPut httput = new HttpPut(urlArrayStrings[0]);
        String cookieString = "";
        httput.addHeader("charset", "UTF-8");
        if (this.currentCookieString.length() > 2) {
            httput.addHeader("Cookie", this.currentCookieString);
        } else if ((null != cookieString) && (cookieString.length() > 3)) {
            httput.addHeader("Cookie", cookieString);
        }
        if ((!GlobalInfo.varibles.containsKey("Cookie")) && (!cookieStrings.equalsIgnoreCase(""))) {
            GlobalInfo.varibles.put("Cookie", cookieStrings);
        } else if (cookieStrings.length() > 1) {
            GlobalInfo.varibles.replace("Cookie", cookieStrings);
        }
        String contentType = this.interfaceTest.getContentType();
        if (contentType.contains("application/json")) {
            httput.addHeader("Content-type", "application/json; charset=utf-8");
            httput.setHeader("Accept", "application/json");
            httput.setEntity(new StringEntity(this.interfaceTest.getBody(), Charset.forName("UTF-8")));
        } else if (urlArrayStrings.length > 1) {
            String[] parmsArry = urlArrayStrings[1].split("&");
            List<NameValuePair> nameValuePairs = new ArrayList(parmsArry.length);
            for (int i = 0; i < parmsArry.length; i++) {
                String valueString = "";
                if (parmsArry[i].split("=").length > 1) {
                    valueString = parmsArry[i].split("=")[1];
                }
                nameValuePairs.add(new BasicNameValuePair(parmsArry[i]
                        .split("=")[0], valueString.replace("\"", "")));
            }
            httput.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
        }
        HttpResponse response = httpclient.execute(httput);
        return response;
    }

    public String doDelete(boolean isRandom, String testApi, InterfaceTestEntity interfaceTest)
            throws Exception {
        String requestUrl = testApi;
        BufferedReader in = null;
        String result = "";
        if (!isRandom) {
            requestUrl = generateRightUrl4Get(testApi, interfaceTest
                    .getInterfaceParams());
        } else {
            requestUrl = generateRandomUrl4Get(testApi, interfaceTest
                    .getInterfaceParams());
        }
        String cookieStrings = this.currentCookieString;
        HttpResponse response = doDelete(requestUrl, cookieStrings);
        String cookies = getCookie(response);
        if (!GlobalInfo.varibles.containsKey("Cookie")) {
            GlobalInfo.varibles.put("Cookie", cookies);
        } else if (cookies.length() > 1) {
            GlobalInfo.varibles.replace("Cookie", cookies);
        }
        String referUrl = "";
        List<String> cookieList = new ArrayList();
        boolean requestOk = true;
        if (response.getStatusLine().toString().contains("Moved Temporarily")) {
            requestOk = false;
        }
        while (!requestOk) {
            if (response.getStatusLine().toString().contains("Moved Temporarily")) {
                Header[] headers = response.getAllHeaders();
                for (Header location : headers) {
                    if (location.getName().equalsIgnoreCase("Set-Cookie")) {
                        cookieList.add(location.getValue());
                    }
                }
                for (Header location : headers) {
                    if (location.getName().equalsIgnoreCase("location")) {
                        referUrl = location.getValue();
                        for (String cookie : cookieList) {
                            this.currentCookieString = (this.currentCookieString + cookie.split(";")[0] + ";");
                        }
                        if (this.currentCookieString.endsWith(";")) {
                            this.currentCookieString = this.currentCookieString.substring(0, this.currentCookieString
                                    .length() - 1);
                        }
                        response = doDelete(referUrl, this.currentCookieString);
                    }
                }
            } else {
                requestOk = true;
            }
        }
        in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        StringBuffer sb = new StringBuffer("");
        String line = "";
        String NL = System.getProperty("line.separator");
        while ((line = in.readLine()) != null) {
            sb.append(line + NL);
        }
        in.close();
        result = sb.toString();
        return result;
    }

    private HttpResponse doDelete(String urlString, String cookieStrings) {
        HttpResponse response = null;

        HttpClient client = new DefaultHttpClient();
        HttpDelete request = new HttpDelete();
        String cookieString = "";
        if (this.currentCookieString.length() > 2) {
            request.addHeader("Cookie", this.currentCookieString);
        } else if ((null != cookieString) && (cookieString.length() > 3)) {
            request.addHeader("Cookie", cookieString);
        }
        if (!GlobalInfo.varibles.containsKey("Cookie")) {
            GlobalInfo.varibles.put("Cookie", cookieStrings);
        } else if (cookieStrings.length() > 1) {
            GlobalInfo.varibles.replace("Cookie", cookieStrings);
        }
        try {
            request.setURI(new URI(urlString));

            response = client.execute(request);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return response;
    }

    private String generateRightUrl4Get(String testApi, List<InterfaceParam> interfaceParams) {
        this.isRightUrl = true;
        String requestUrl = testApi;
        for (InterfaceParam inParam : interfaceParams) {
            inParam.setRealType(inParam.getType());
        }
        requestUrl = generateTheUrl(testApi, interfaceParams);
        return requestUrl;
    }

    private String generateRandomUrl4Get(String testApi, List<InterfaceParam> interfaceParams) {
        String requestUrl = testApi;
        requestUrl = requestUrl + "?";
        String paramString = randomParameters(interfaceParams);

        requestUrl = paramString;

        return requestUrl;
    }

    private String randomParameters(List<InterfaceParam> paraList) {
        String requestUrl = this.testApi;
        this.isRightUrl = true;
        int randomType = 2;
        int randomParmCount = paraList.size();
        List<InterfaceParam> temParaList = new ArrayList();
        int i;
        if (paraList.size() > 0) {
            randomParmCount = (int) (Math.random() * paraList.size());
            if (randomParmCount != paraList.size()) {
                for (i = 0; i < randomParmCount; i++) {
                    InterfaceParam interfaceParam = (InterfaceParam) paraList.get(
                            (int) (Math.random() * paraList.size()));
                    if (temParaList.size() == 0) {
                        InterfaceParam temParaEntity = (InterfaceParam) interfaceParam.clone();
                        temParaList.add(temParaEntity);
                    }
                    for (int j = 0; j < temParaList.size(); j++) {
                        if (interfaceParam.getName().equalsIgnoreCase(((InterfaceParam) temParaList.get(j)).getName())) {
                            i--;
                            break;
                        }
                        if (j == temParaList.size() - 1) {
                            InterfaceParam temParaEntity = (InterfaceParam) interfaceParam.clone();
                            temParaList.add(temParaEntity);
                            break;
                        }
                    }
                }
            } else {
                temParaList = paraList;
            }
            if (null != temParaList) {
                for (InterfaceParam inpara : temParaList) {
                    String value = null;
                    String typeString = this.typeArray[((int) (Math.random() * randomType))];
                    if (typeString.equalsIgnoreCase("String")) {
                        value = randomString((int) (Math.random() * 20.0D));
                        inpara.setRealType("string");
                    } else if (typeString.equalsIgnoreCase("int")) {
                        value = randomNumber() + "";
                        inpara.setRealType("int");
                    }
                    inpara.setValue(value);
                }
            }
        }
        if (null != temParaList) {
            for (InterfaceParam inpara : paraList) {
                for (int io = 0; io < temParaList.size(); io++) {
                    if (((InterfaceParam) temParaList.get(io)).getName().equalsIgnoreCase(inpara.getName())) {
                        break;
                    }
                    if ((io == temParaList.size() - 1) &&
                            (inpara.getNotNull())) {
                        this.isRightUrl = false;
                        break;
                    }
                }
            }
            if (this.isRightUrl) {
                for (InterfaceParam subInpara : temParaList) {
                    if ((!subInpara.getType().equalsIgnoreCase("string")) &&
                            (!subInpara.getRealType().equalsIgnoreCase(subInpara
                                    .getType()))) {
                        this.isRightUrl = false;
                        break;
                    }
                }
            }
        }
        if ((paraList.size() > 0) && (
                (null == temParaList) || (temParaList.size() == 0))) {
            this.isRightUrl = false;
        }
        return generateTheUrl(this.testApi, temParaList);
    }

    private String generateTheUrl(String testApi, List<InterfaceParam> paraList) {
        String requestUrl = testApi;
        if (paraList.size() > 0) {
            requestUrl = requestUrl + "?";
            String paramString = "";
            for (InterfaceParam inParam : paraList) {
                String paraValue = getVarible(inParam.getValue());
                if (inParam.isRandom()) {
                    paraValue = getUnixTime() + "";
                }
                paramString = paramString + "&" + inParam.getName() + "=" + paraValue;
            }
            paramString = paramString.substring(1);
            requestUrl = requestUrl + paramString;
        }
        this.url = requestUrl;
        return requestUrl;
    }

    private String randomString(int length) {
        String generateString = "";
        for (int i = 0; i < length; i++) {
            generateString = generateString + randomChar();
        }
        return generateString;
    }

    private String randomChar() {
        String lowChars = "abcdefghijklmnopqrstuvwxyz";
        int toUpChar = (int) (Math.random() * 2.0D);
        String generateChar = "a";
        if (toUpChar == 1) {
            generateChar = lowChars.charAt((int) (Math.random() * 26.0D)) + "";
            generateChar = generateChar.toUpperCase();
        } else {
            generateChar = lowChars.charAt((int) (Math.random() * 26.0D)) + "";
        }
        return generateChar;
    }

    private int randomNumber() {
        return (int) (Math.random() * 100000.0D);
    }

    private long getUnixTime() {
        return System.currentTimeMillis() / 100L;
    }

    private Object transformDataType(String value, String type) {
        String lowType = type.toLowerCase();
        switch (lowType) {
            case "string":
                return value.toString();
            case "int":
                return Integer.valueOf(Integer.parseInt(value));
        }
        return value.toString();
    }

    public boolean getIsRightUrl() {
        return this.isRightUrl;
    }

    public String getUrl() {
        return this.url;
    }

    private String getCookie(HttpResponse response) {
        String cookieString = "";
        List<String> cookieList = new ArrayList();
        Header[] headers = response.getAllHeaders();
        for (Header location : headers) {
            if (location.getName().equalsIgnoreCase("Set-Cookie")) {
                cookieList.add(location.getValue());
            }
        }

        Iterator<String> iterator = cookieList.iterator();

        while (iterator.hasNext()){
            String cookie = (String) iterator.next();
            if (cookie.endsWith(";")) {
                cookieString = cookieString + cookie;
            } else {
                cookieString = cookieString + cookie + ";";
            }
        }

        if (cookieString.endsWith(";")) {
            cookieString = cookieString.substring(0, cookieString.length() - 1);
        }
        return cookieString;
    }

    public String getVarible(String varibleName) {
        String VaribleValue = "";
        if (varibleName.startsWith("$")) {
            String valueString = "";
            if (varibleName.contains(".")) {
                valueString = (String) GlobalInfo.varibles.get(varibleName.substring(1)
                        .split("\\.")[0]);
            } else {
                valueString = (String) GlobalInfo.varibles.get(varibleName.substring(1));
            }
            if (varibleName.contains(".")) {
                String realVarible = varibleName.substring(1).split("\\.")[1];
                if ((valueString.contains(";")) && (!valueString.contains("{")) && (!valueString.contains(":"))) {
                    String[] variblesStrings = valueString.split(";");
                    for (String varible : variblesStrings) {
                        if (varible.split("=")[0].equalsIgnoreCase(realVarible)) {
                            VaribleValue = varible.split("=")[1];
                        }
                    }
                } else {
                    int fistIndex;
                    int secondIndex;
                    if ((valueString.contains("{")) && (valueString.contains(":"))) {
                        String subString = valueString.substring(valueString.indexOf(realVarible));
                        fistIndex = subString.indexOf(",");
                        secondIndex = subString.indexOf("}");
                        if (fistIndex < secondIndex) {
                            VaribleValue = subString.split(",")[0].split(":")[1].replace("\"", "");
                        } else {
                            VaribleValue = subString.split("}")[0].split(":")[1].replace("\"", "");
                        }
                    } else if (valueString.contains("=")) {
                        if (valueString.contains(";")) {
                            String[] variblesStrings = valueString.split(";");
                            for (String varible : variblesStrings) {
                                if (varible.split("=")[0].equalsIgnoreCase(realVarible)) {
                                    VaribleValue = varible.split("=")[1];
                                }
                            }
                        } else if (valueString.split("=")[0].equalsIgnoreCase(realVarible)) {
                            VaribleValue = valueString.split("=")[1];
                        }
                    }
                }
            } else {
                VaribleValue = valueString;
            }
        } else {
            VaribleValue = varibleName;
        }
        return VaribleValue;
    }
}
